package com.younion.action.consumer.impl;

import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import com.younion.action.consumer.AbstractRequestConsumer;
import com.younion.action.interactive.mobile.ApiConstant;
import com.younion.action.interactive.mobile.ResultObject;
import com.younion.business.ICustomerService;
import com.younion.business.IFavourableEntityService;
import com.younion.business.IFavourableService;
import com.younion.business.IMemberCardService;
import com.younion.business.IRegisterUserService;
import com.younion.model.FavOperateResult;
import com.younion.util.HttpRequestUtils;
import com.younion.util.JsonUtils;
import com.younion.util.UUIDUtil;
import com.younion.vo.CustomerVO;
import com.younion.vo.FavourableEntityVO;
import com.younion.vo.FavourableVO;
import com.younion.vo.RegisterUserVO;

public class FavourableApllyConsumer extends AbstractRequestConsumer {

	private IMemberCardService memberCardService;
	private IFavourableService favourableService;
	private IFavourableEntityService favourableEntityService;
	private IRegisterUserService registerUserService;
	private ICustomerService customerService;
	private UUIDUtil uUIDUtil = new UUIDUtil();

	@Override
	public String execute(HttpServletRequest request,
			HttpServletResponse response) throws Exception {
		return applyFav(request, response);
	}

	public String applyFav(HttpServletRequest request,
			HttpServletResponse response) {
		String user_token = request.getParameter("user_token");
		String fav_ent_id = request.getParameter("fav_ent_id");
		if (!HttpRequestUtils.checkNotEmptyParam("user_token", user_token,
				response)) {
			return null;
		}
		if (!HttpRequestUtils.checkNotEmptyParam("fav_ent_id", fav_ent_id,
				response)) {
			return null;
		}
		if (!HttpRequestUtils.checkNeedIntegerParam("fav_ent_id", fav_ent_id,
				response)) {
			return null;
		}
		ResultObject resultObject = new ResultObject();
		try {
			FavourableEntityVO favourableEntity = favourableEntityService
					.getById(fav_ent_id);
			CustomerVO customerVO = customerService.getById(favourableEntity
					.getCustomerId().toString());
			if (customerVO.getCooperationStaus() != null
					&& customerVO.getCooperationStaus() == 0) {
				resultObject.setStatus(ApiConstant.SUCCESS_STATUS);
				resultObject.setMsg("该商家与优联尚未合作，请联系商家开通服务");
				HttpRequestUtils.printContent(JsonUtils
						.parseObj2Json(resultObject), response);
				return null;
			}
			Date applyBegDate = favourableEntity.getApplyBegDate();
			Date applyEndDate = favourableEntity.getApplyEndDate();
			Date nowDate = new Date();
			FavOperateResult result = new FavOperateResult();
			Map map = new HashMap();
			map.put("userToken", user_token);
			List<RegisterUserVO> registerUserVOList = (List<RegisterUserVO>) registerUserService
					.getByMap(map);
			RegisterUserVO user = new RegisterUserVO();
			if (registerUserVOList.size() > 0) {
				user = registerUserVOList.get(0);
			} else {
				resultObject.setStatus(ApiConstant.FALSE_STATUS);
				resultObject.setMsg(ApiConstant.PUBLIC_EXCEPTION);
				HttpRequestUtils.printContent(JsonUtils
						.parseObj2Json(resultObject), response);
				return null;
			}
			// 用户未申请过该商家的会员卡
			if (memberCardService.getCountByMap(map) == 0) {
				result.setSuccessful(ApiConstant.FALSE_STATUS);
				resultObject.setStatus(ApiConstant.SUCCESS_STATUS);
				resultObject.setMsg(ApiConstant.FAV_FORCE_BE_MEMBER);
				resultObject.setResult(result);
				HttpRequestUtils.printContent(JsonUtils
						.parseObj2Json(resultObject), response);
				return null;
			}
			// 早于申请有效期
			if (applyBegDate != null) {
				if (nowDate.before(applyBegDate)) {
					result.setSuccessful(ApiConstant.FALSE_STATUS);
					resultObject.setStatus(ApiConstant.SUCCESS_STATUS);
					resultObject.setMsg(ApiConstant.FAV_APPLY_TOO_EARLY);
					resultObject.setResult(result);
					HttpRequestUtils.printContent(JsonUtils
							.parseObj2Json(resultObject), response);
					return null;
				}
			}
			// 晚于申请有效期
			if (applyEndDate != null) {
				if (nowDate.after(applyEndDate)) {
					result.setSuccessful(ApiConstant.FALSE_STATUS);
					resultObject.setStatus(ApiConstant.SUCCESS_STATUS);
					resultObject.setMsg(ApiConstant.FAV_APPLY_TOO_LATE);
					resultObject.setResult(result);
					HttpRequestUtils.printContent(JsonUtils
							.parseObj2Json(resultObject), response);
					return null;
				}
			}
			// 到达申请数量限制
			if (favourableEntity.getAmount() == 0) {
				result.setSuccessful(ApiConstant.FALSE_STATUS);
				resultObject.setStatus(ApiConstant.SUCCESS_STATUS);
				resultObject.setMsg(ApiConstant.FAV_HAVE_NO_MORE);
				resultObject.setResult(result);
				HttpRequestUtils.printContent(JsonUtils
						.parseObj2Json(resultObject), response);
				return null;
			}
			FavourableVO favourable = new FavourableVO();
			favourable.setCustomerId(Integer.valueOf(favourableEntity
					.getCustomerId()));
			favourable.setFavourableEntity(favourableEntity);
			String code = uUIDUtil.getUuid().toString();
			favourable.setCode(code);
			favourable.setState(0);
			favourable.setGainManner(1);
			favourable.setGainTime(nowDate);
			favourable.setUserId(user.getId());
			favourableService.save(favourable);
			map.put("userId", user.getId());
			map.put("customerId", favourableEntity.getCustomerId());
			// 增加会员卡的优惠券数量
			memberCardService.updateCouponAmount(map);
			// memberCardService.updateCouponAmountById(memberCard.getId().toString());
			// 减去优惠券实体数量
			if (favourableEntity.getAmount() != null
					&& !"".equals(favourableEntity.getAmount())) {
				favourableEntityService.modifyAmount(favourableEntity.getId()
						.toString());
			}
			result.setSuccessful(ApiConstant.SUCCESS_STATUS);
			resultObject.setStatus(ApiConstant.SUCCESS_STATUS);
			resultObject.setMsg(ApiConstant.FAV_APPLY_SUCCESS);
			resultObject.setResult(result);
		} catch (Exception e) {
			resultObject.setStatus(ApiConstant.FALSE_STATUS);
			resultObject.setMsg(ApiConstant.PUBLIC_EXCEPTION);
			e.printStackTrace();
		}
		HttpRequestUtils.printContent(JsonUtils.parseObj2Json(resultObject),
				response);
		return null;
	}

	public void setMemberCardService(IMemberCardService memberCardService) {
		this.memberCardService = memberCardService;
	}

	public void setFavourableService(IFavourableService favourableService) {
		this.favourableService = favourableService;
	}

	public void setFavourableEntityService(
			IFavourableEntityService favourableEntityService) {
		this.favourableEntityService = favourableEntityService;
	}

	public void setRegisterUserService(IRegisterUserService registerUserService) {
		this.registerUserService = registerUserService;
	}

	public void setCustomerService(ICustomerService customerService) {
		this.customerService = customerService;
	}

}
